package com.dashan.backend.datastore;
import static com.dashan.backend.geosearch.GeoMath.*;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.dashan.backend.geosearch.GeoCell;
import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@SuppressWarnings("unused")
public class DsGeoCellArray {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    
    @Persistent
    private long cellNumberL00;
    @Persistent
    private long cellNumberL01;
    @Persistent
    private long cellNumberL02;
    @Persistent
    private long cellNumberL03;
    @Persistent
    private long cellNumberL04;
    @Persistent
    private long cellNumberL05;
    @Persistent
    private long cellNumberL06;
    @Persistent
    private long cellNumberL07;
    @Persistent
    private long cellNumberL08;
    @Persistent
    private long cellNumberL09;
    @Persistent
    private long cellNumberL10;
    @Persistent
    private long cellNumberL11;
    @Persistent
    private long cellNumberL12;
    @Persistent
    private long cellNumberL13;
    @Persistent
    private long cellNumberL14;
    @Persistent
    private long cellNumberL15;
    @Persistent
    private long cellNumberL16;
    @Persistent
    private long cellNumberL17;
    @Persistent
    private long cellNumberL18;
    @Persistent
    private long cellNumberL19;
    @Persistent
    private long cellNumberL20;
    @Persistent
    private long cellNumberL21;
    @Persistent
    private long cellNumberL22;
    @Persistent
    private long cellNumberL23;
    @Persistent
    private long cellNumberL24;
    @Persistent
    private long cellNumberL25;
    @Persistent
    private long cellNumberL26;
    @Persistent
    private long cellNumberL27;
    @Persistent
    private long cellNumberL28;
    @Persistent
    private long cellNumberL29;
    @Persistent
    private long cellNumberL30;
    @Persistent
    long cellNumberL31;

    public DsGeoCellArray(double lon, double lat) {
        GeoCell cell = new GeoCell(lon, lat);
        try {
            for (int i = 0; i <= MAX_LEVEL; i++) {
                DsGeoCellArray.class.getDeclaredField(getFieldName(i)).
                    setLong(this, cell.getMortonNumber() >> (MAX_LEVEL - i) * 2);
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    
    public long getCellNumber(int i) {
        try {
            long cellNum = DsGeoCellArray.class.
            getDeclaredField(getFieldName(i)).getLong(this);
            return cellNum;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return -1;
        
    }

    public static String getFieldName(int i) {
        return "cellNumberL" + String.format("%02d", i);
    }
}
